package com.pl.lc.user.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pl.lc.common.param.PayAdd;
import com.pl.lc.common.param.UserVipAdd;
import com.pl.lc.common.vo.R;
import com.pl.lc.core.alipay.AliPayUtil;
import com.pl.lc.user.entity.PayLog;
import com.pl.lc.user.entity.Vip;
import com.pl.lc.user.entity.VipOrder;
import com.pl.lc.user.mapper.VipMapper;
import com.pl.lc.user.service.VipOrderService;
import com.pl.lc.user.service.VipService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pl.lc.user.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 * 8.用户收费会员表 服务实现类
 * </p>
 *
 * @author pl
 * @since 2024-06-11
 */
@Service
public class VipServiceImpl extends ServiceImpl<VipMapper, Vip> implements VipService {
    @Autowired
    private VipOrderService vipOrderService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private VipMapper vipMapper;


    /**
     * 使用钱包余额购买的vip的业务
     *
     * @param userVipAdd
     * @return
     */
    @Override
    public R buyVipByWallet(UserVipAdd userVipAdd) {
        // 1,创建一个会员的订单
        VipOrder vipOrder = vipOrderService.createVipOrder(userVipAdd);
        // 2.扣减账户余额 记录日志
        walletService.descBalance(userVipAdd.getUserId(), userVipAdd.getMoney());
        // 3.修改会员订单状态
        vipOrder.setStatus(2);
        vipOrderService.updateById(vipOrder);
        //4.创建，购买 或者 续费 vip会员
        boolean flag = createVip(userVipAdd);
        //5.返回
        if (flag) {
            return R.ok("购买成功");
        }
        return R.fail("购买失败");

    }

    /**
     * 创建购买或者续费vip
     * @param userVipAdd
     * @return
     */
    public  boolean createVip(UserVipAdd userVipAdd) {
        //    首先查询是否有vip
        VipOrder vipOrder = new VipOrder();
        QueryWrapper<Vip> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userVipAdd.getUserId());
        Vip vip = vipMapper.selectOne(queryWrapper);
        //     判断以下情况
        if (vip == null) {
            //没有vip创建vip
            vip = new Vip();
            vip.setUserId(userVipAdd.getUserId());
            vip.setInfo("购买vip");
            vip.setType(userVipAdd.getVipType());
            vip.setDays(365);
            vip.setCtime(new Date());
            // 开始时间
            vip.setSdate(new Date());
            //    结束时间
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.DAY_OF_MONTH, 365);
            Date time = instance.getTime();
            vip.setEdate(time);
            int num = vipMapper.insert(vip);

            if (num>0){
                return  true;
            }
        } else {
            //    该账户有vip，续费vip
            vip.setInfo("续费vip");
            vip.setType(userVipAdd.getVipType());
            //    1.已经过期续费
            //设置为原有会卡的结束时间再加 365
            if (vip.getEdate().before(new Date())) {
                vip.setDays(365);
                vip.setSdate(new Date());

                Calendar calendar = Calendar.getInstance(); //当前是时间
                calendar.add(Calendar.DAY_OF_MONTH, 365);
                Date eDate = calendar.getTime();  // 得到一年后的今天
                vip.setEdate(eDate);
            } else {
                //    2.未过期续费
                //当前的时间
                Calendar calendar = Calendar.getInstance();
                //设置上次结束时间为这次的开始时间
                calendar.setTime(vip.getEdate());
                calendar.add(Calendar.DAY_OF_MONTH, 365);
                Date eDate = calendar.getTime();  // 得到 在加一年后的今天
                vip.setEdate(eDate);
                long betweenDay = DateUtil.between(new Date(), vip.getEdate(), DateUnit.DAY);
                vip.setDays((int) (betweenDay + 365));
            }
            //    更新vip
            int i = vipMapper.updateById(vip);
            if (i > 0) {
                return true;
            }
        }
        return false;
    }

    @Autowired
    private RedisTemplate  redisTemplate;
    @Override
    public R buyVipByZfb(UserVipAdd userVipAdd) {
        // 1,创建一个会员的订单
        VipOrder vipOrder = vipOrderService.createVipOrderByZfb(userVipAdd);
        // 2.扣减账户余额 记录日志
        PayLog payLog =walletService.buyVipByZfb(userVipAdd);
        if (payLog!=null){
            // 调用支付宝支付
            redisTemplate.opsForValue().set("vipOrder",vipOrder);
            redisTemplate.opsForValue().set("userVipAdd",userVipAdd);
            String html = AliPayUtil.createPay(payLog.getNo(), payLog.getMoney(), "vip充值");

            return R.ok(html);// 前端看到支付宝 界面
        }else {
            return R.fail("支付失败");
        }
        // 3.修改会员订单状态
        // vipOrder.setStatus(2);
        // vipOrderService.updateById(vipOrder);
        // //4.创建，购买 或者 续费 vip会员
        // boolean flag = createVip(userVipAdd);
        // //5.返回
        // if (flag) {
        //     return R.ok("购买成功");
        // }
        // return R.fail("购买失败");
    // return R.ok();
    }
}
